﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Ports;
using System.Security.Cryptography;
using System.Threading;

namespace ayxjDllClass
{
    public  class AGC
    {
        private SerialPort serialPort;
        private bool boolect=false;
        private Thread receiveThread;

        //AGC信号
        private double voltageAgc=0;
        private double voltageFreq=0;
        //队列
        static ConcurrentQueue<byte[]> queue = new ConcurrentQueue<byte[]>();
        private void ReceiveData()
        {
            byte[] JHdataDI = new byte[] { 0x7b, 0x20, 0x3f, 0x7d };//agc状态
            byte[] JHdataAL = new byte[] { 0x7b, 0x08, 0x3f, 0x7d };//频率状态
            while (true)
            {
                try
                {
                    if (!boolect) { serialPort.Close(); serialPort.Dispose(); break; }
                    if (queue.Count == 0)
                    {
                        // queue.Enqueue(JHdataDI);
                        queue.Enqueue(JHdataAL);
                    }
                    queue.TryDequeue(out byte[] sead);
                    serialPort.Write(sead, 0, sead.Length);
                    Thread.Sleep(10);
                    if (serialPort.BytesToRead > 0)
                    {
                        byte[] receivedData = new byte[serialPort.BytesToRead];
                        int num = serialPort.Read(receivedData, 0, receivedData.Length);

                        //if (num == 6 && receivedData[0] == 0x7B && receivedData[1] == 0x0F && receivedData[2] == 0x3D && receivedData[5] == 0x7D)
                        //{

                        //    byte[] voide = new byte[2];
                        //    Array.Copy(receivedData, 3, voide, 0, 2);
                        //    Array.Reverse(voide);
                        //    voltageAgc = BitConverter.ToInt16(voide, 0) / 100.000;
                        //}
                        if (num >= 6 && receivedData[0] == 0x7B && receivedData[1] == 0x0F && receivedData[2] == 0x3D && receivedData[5] == 0x7D)
                        {

                            byte[] voide = new byte[2];
                            Array.Copy(receivedData, 3, voide, 0, 2);
                            Array.Reverse(voide);
                            voltageAgc = BitConverter.ToInt16(voide, 0) / 100.000;
                        }
                        else if (num >= 8 && receivedData[0] == 0x7B && receivedData[1] == 0x08 && receivedData[2] == 0x3D && receivedData[7] == 0x7D)
                        {
                            byte[] voide = new byte[4];
                            Array.Copy(receivedData, 3, voide, 0, 4);
                            Array.Reverse(voide);
                            voltageFreq = BitConverter.ToUInt32(voide, 0) / 1000000.000;
                        }
                    }
                }
                catch
                {
                }
            }
        }
        public bool Open(string portName, int baudRate)
        {
            try
            {
                if (serialPort == null || !serialPort.IsOpen)
                {
                    serialPort = new SerialPort(portName, baudRate);
                    serialPort.Open();
                    boolect = true;
                    receiveThread = new Thread(ReceiveData) { IsBackground = true };
                    receiveThread.Start();
                    return true;
                }
                else { return true; }
            }
            catch{ return false; }
        }

        public bool Close()
        {
            try
            {
                if (serialPort != null && serialPort.IsOpen)
                {
                    boolect = false;
                    return true;
                }
                else { return true; }
            }
            catch { return false; }
        }

        public double GetAGC()
        {
            return voltageAgc;
        }
        public double GetFreq()
        {
            return voltageFreq;
        }
        
        public bool SetFreq( double freq)
        {
            byte[] data =BitConverter.GetBytes(Convert.ToUInt32(freq*1000000.0));
            Array.Reverse(data);
            byte[] beacon = new byte[] { 0x7B, 0x08, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x7D };
            data.CopyTo(beacon, 3);
            queue.Enqueue(beacon);
            return true;
        }
    }
}
